package com.liangtian.mdm.dm.sql.factory;


import com.liangtian.mdm.dm.attribute.model.Attribute;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.sql.enums.Column;
import com.liangtian.mdm.dm.sql.enums.Component;

import java.util.*;

/**
 * 系统属性工厂
 *
 * @author honye
 */
public class SystemAttributeFactory {

    /**
     * 生成列
     *
     * @param modelId
     * @param col
     * @return
     */
    public static Attribute genAttribute(String modelId, Column col) {
        Attribute ta = new Attribute();
        ta.setModelId(modelId);
        ta.setAttrColumn(col.getName());
        ta.setAttrDesc(col.getDesc());
        ta.setAttrLength("20");
        ta.setDataType(Component.TEXT.getName());

        switch (col) {
            case CREATETIME:
                ta.setDataType(Component.DATETIME.getName());
                break;
            case UPDATETIME:
                ta.setDataType(Component.DATETIME.getName());
                break;
        }

        return ta;
    }

    /**
     * 系统所有列
     *
     * @param modelId
     * @return
     */
    public static List<Attribute> getSystemAttributes(String modelId) {

        List<Attribute> las = new ArrayList<Attribute>();
        Column[] sysCols = Column.values();
        for (Column col : sysCols) {
            las.add(genAttribute(modelId, col));
        }
        return las;
    }

    /**
     * 返回有序的系统列
     *
     * @return
     */
    public static List<Column> getSystemColumns() {

        Column[] cls = Column.values();
        List<Column> lcs = Arrays.asList(cls);
        Collections.sort(lcs, new Comparator<Column>() {
            @Override
            public int compare(Column o1, Column o2) {
                //按Order升序
                return o1.getOrder() - o2.getOrder();
            }
        });
        return lcs;
    }

    /**
     * 返回系统列的列名字符串
     *
     * @return
     */
    public static StringBuilder getSystemColumnsBuilder() {
        //List<Column> lcs = getSystemColumns();
        Column[] cls = Column.values();
        StringBuilder builder = new StringBuilder();
        for (Column col : cls) {
            String colName = col.getName();
            if (col.getType().startsWith("DATE"))
                builder.append("to_char(" + colName + ", 'yyyy-mm-dd') as ").append(colName).append(",");
            else
                builder.append(colName).append(",");
        }
        return builder;
    }

    /**
     * 返回给定集合的列名
     *
     * @param las
     * @return
     */
    public static StringBuilder getColumnBuilder(List<Attribute> las) {
        StringBuilder builder = new StringBuilder();
        for (Attribute attr : las) {
            String colName = attr.getAttrColumn();
            String colType = attr.getDataType();
/*			
 * 			// 所有数据都存 字符串
  			if(colType.startsWith("DATE"))
				builder.append("to_char("+colName+", 'yyyy-mm-dd') as ").append(colName).append(",");
			else */
            if (colType.endsWith("LOB")) {
                continue;
            } else
                builder.append(colName).append(",");
        }
        return builder;
    }

    public static String getColumnByConfig(List<AttrConfig> acLst) {
        StringBuffer sb = new StringBuffer();
        for (AttrConfig ac : acLst) {
            String colName = ac.getAttrColumn();
            String colType = ac.getDataType();
/*			
 * 			// 所有数据都存 字符串
  			if(colType.startsWith("DATE"))
				builder.append("to_char("+colName+", 'yyyy-mm-dd') as ").append(colName).append(",");
			else */
            if (colType.endsWith("LOB")) {
                continue;
            } else
                sb.append(colName).append(",");
        }
        return sb.substring(0, sb.length() - 1);
    }
}
